package com.bookshare.service.base.impl;

import com.bookshare.dao.base.BookMapper;
import com.bookshare.dao.entity.Book;
import com.bookshare.dao.entity.BookClassification;
import com.bookshare.dao.entity.BookClassificationExample;
import com.bookshare.dao.entity.BookExample;
import com.bookshare.dao.ext.BookExtMapper;
import com.bookshare.service.base.IBookCatalogueService;
import com.bookshare.service.model.BookModel;
import com.bookshare.service.util.ModelConvert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class BookCatalogueService implements IBookCatalogueService {
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private BookExtMapper bookExtMapper;

    public Map<String, String> queryClassificationAndCount() {
        List<Map<String, String>> list = bookExtMapper.selectCountGroupByClassification();
        Map<String, String> map = new HashMap<String, String>();
        for (int i = 0; i < list.size(); i++) {
            String key = list.get(i).get("classification");
            String value = String.valueOf(list.get(i).get("count"));
            map.put(key, value);
        }
        return map;
    }

    public List<BookModel> queryAllBookForCatalogue(int pageNum){
        List<BookModel> returnList = new ArrayList<BookModel>();
        List<Book> list = new ArrayList<Book>();
        BookExample bookExample = new BookExample();
        ModelConvert modelConvert = new ModelConvert();

        PageHelper.startPage(pageNum,10);
        list = bookMapper.selectByExample(bookExample);
        for (Book book:list){
            BookModel bookModel = modelConvert.ConvertToBookModel(book);
            returnList.add(bookModel);
        }
        BookModel bookModel = new BookModel();
        return returnList;
    }

    public String querySumPage(){
        Double sumBook = Double.parseDouble(bookExtMapper.selectSumBook());
        int sumPage = (int)Math.floor(sumBook/10);
        if(sumBook%10 != 0){
            sumPage = sumPage+1;
        }
        return String.valueOf(sumPage);
    }

//    public String querySumPage(){
//        int sumBook = Integer.parseInt(bookExtMapper.selectSumBook());
//        int temp = sumBook%10;
//        int sumPage = temp + (sumBook - temp)/10;
//        return String.valueOf(sumPage);
//    }
}
